# -*- coding: UTF-8 -*-
'''
Created on 28.02.2013

@author: Peter He�
'''
import unittest
from time import time

import SetEnv as env  # build global variables for pathes

import sys

from spreads.LinearSpreadGen import LinearSpread 
from spreads.LinearSpreadGen import SpreadType
from animation.Animation import LFO

class Test(unittest.TestCase):


    def setUp(self):
        pass


    def tearDown(self):
        pass

    # more about xml: http://docs.python.org/2/library/xml.etree.elementtree.html#tutorial
    def test001ReadingVVVVFile(self):
        print "============================================================="  
        print "test001ReadingVVVVFile"      
        print "============================================================="
        import xml.etree.ElementTree as ET
        tree = ET.parse(env._base + '/001TestXml.v4p')
        root = tree.getroot()
        self.assertEqual("PATCH",root.tag,"Not a vvvv file?")
        print "root.tag: ",root.tag                      # http://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element
        print "-------------------------------------------------------------"
        for child in root:
            print child.tag, child.attrib
        pass
        print "-------------------------------------------------------------"
        print "show all NODEs"
        for node in root.iter('NODE'):
            print node.attrib
        self.assertIs(len(node.attrib), 4)
        print "-------------------------------------------------------------"
        print "show all LINKs"
        for node in root.iter('LINK'):
            print node.attrib
        self.assertIs(len(node.attrib), 4)

    # test 002 obsolete

    def test003CreateObjectsDynamic(self):
        print "============================================================="
        print "test003CreateObjectsDynamic"
        print "============================================================="
        obj = globals()['LinearSpread']
        print obj
        self.assertIsNotNone(obj, "Dynamic creation failed")

    def test004CreateNodesFromFile(self):
        
        print "============================================================="
        print "test004CreateNodesFromFile"
        print "============================================================="
        import xml.etree.ElementTree as ET
        tree = ET.parse(env._base + '/001TestXml.v4p')
        root = tree.getroot()
        allNodesObjects={}
        counter=1
        
        for node in root.iter('NODE'):
            obj = globals()['LinearSpread']()
            nodeid = node.attrib['id']
            allNodesObjects[nodeid] = obj
            obj.update(SpreadCount_in=[counter])
            counter += 1
            
        for ls in allNodesObjects.itervalues():
            print ls.Output
            
        # or
        
        for key in allNodesObjects:
            print "key=", key, " value=", allNodesObjects[key]
            
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testReadingVVVVFile']
    unittest.main()